/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/

<%@ page language="java" import="java.util.*,java.text.*"%>
<%@ page import="java.net.MalformedURLException" %>
<%@ page import="java.net.URL" %>
<%@ page import="com.google.gdata.client.calendar.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.client.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.model.*" %>
<%@ page import="com.google.gdata.util.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.data.extensions.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.client.http.*" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="SelfBookingSystem.AdminValues" %>
<%@ page import="SelfBookingSystem.PMF" %>
<%@ page import="SelfBookingSystem.UserToAppID" %>
<%@ page import="org.apache.commons.configuration.*" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="org.apache.commons.configuration.*" %>



<%!
public int nullIntconv(String inv)
{   
	int conv=0;
		
	try{
		conv=Integer.parseInt(inv);
	}
	catch(Exception e)
	{}
	return conv;
}

%>
<!--If this page is not called embedded in index.php forward to index.php-->
<script type="text/javascript">
if(window.location.pathname.indexOf("index") == -1){
	window.location.href = "index.jsp";
}
</script>
<%
//Variablen Deklaration
String GoogleCalendardURL;
String GoogleEmail;
String Password;
String tmp;
String value;
String BadCredentials = new String();;
String Credential = new String();
String AppID = new String();

ArrayList AL_AppID = new ArrayList(); 
ArrayList tmp_AppID = new ArrayList(); 
ArrayList EmailAddress = new ArrayList();
ArrayList GoogleCalendarURL = new ArrayList();


System.out.print(request.getLocalName() );
    
if(request.getParameter("AppID") != null){
	AppID = request.getParameter("AppID");
}else{
	AppID = "xxx";
}

//Get Userdata
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();	



try
{
    XMLConfiguration fields = new XMLConfiguration("config/AdminFields.xml");
    // do something with config

	List list = fields.configurationsAt("tables.table.fields.field");
	int i=0;
	for(Iterator it = list.iterator(); it.hasNext();)
	{
		tmp=null;
	    HierarchicalConfiguration sub = (HierarchicalConfiguration) it.next();
		 
		i++;   
	}
}

catch(ConfigurationException ex)
{
    // something went wrong, e.g. the file was not found
}


		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		String query = new String();
		
		query = "select from " + UserToAppID.class.getName() + " where User == \"" + user.getNickname().toLowerCase() + "\" && Level == \"20\"";
		List<UserToAppID> UserValues = (List<UserToAppID>) pm.newQuery(query).execute();

		if (UserValues.isEmpty()) {
		
		}else{
			for (UserToAppID uv : UserValues) {
				tmp_AppID.add(uv.getAppID()); 
			}
			//System.out.print("AppID" + AL_AppID.toString() );
			
			//Give me all AppID's
    		Query query1 = pm.newQuery(AdminValues.class, ":p.contains(AppID)");
    		List<AdminValues> ConfigurationValues = (List<AdminValues>) query1.execute(tmp_AppID);
    	
    	
			//query = "select from " + AdminValues.class.getName();
			//List<AdminValues> ConfigurationValues = (List<AdminValues>) pm.newQuery(query).execute();
			if (ConfigurationValues.isEmpty()) {
			
			}else{
				for (AdminValues cg : ConfigurationValues) {
					EmailAddress.add(cg.getEmailAddress());
					GoogleCalendarURL.add(cg.getGoogleCalendarURL());
					AL_AppID.add(cg.getAppID());				
				}
			}
		}
%>

<table border=1>
		<%for(int j=0;j<AL_AppID.size();j++){%>
			<tr>
				<td><a href="index.jsp?AppID=<%=AL_AppID.get(j)%>&Tab=ConnectionData"><%=AL_AppID.get(j)%></a></td>		
				<td><%=EmailAddress.get(j)%></td>		
				<td><%=GoogleCalendarURL.get(j)%></td>
				<td><a href="/DeleteApp?AppID=<%=AL_AppID.get(j)%>">Delete</a></td>		
			</tr>
		<%}%>					
			<tr>
				<td rowspan=3><a href="index.jsp?AppID=new&Tab=ConnectionData">New</a></td>		
			</tr>
</table>